﻿using System.Windows;
using System.Windows.Controls;
using X.Infrastructure.Enums;

namespace X.WPF.Controls.Helpers
{
    /// <summary>
    /// 边框帮助类
    /// </summary>
    public class BorderHelper
    {
        #region # 字段及构造器

        /// <summary>
        /// 静态构造器
        /// </summary>
        static BorderHelper()
        {
            // 注册附加属性
            BorderHelper.LeftBorderThicknessProperty = DependencyProperty.RegisterAttached("LeftBorderThickness", typeof(Thickness), typeof(BorderHelper), new PropertyMetadata(new Thickness(0), BorderHelper.LeftBorderThicknessChangedCallback));
            BorderHelper.RightBorderThicknessProperty = DependencyProperty.RegisterAttached("RightBorderThickness", typeof(Thickness), typeof(BorderHelper), new PropertyMetadata(new Thickness(0), BorderHelper.RightBorderThicknessChangedCallback));
            BorderHelper.TopBorderThicknessProperty = DependencyProperty.RegisterAttached("TopBorderThickness", typeof(Thickness), typeof(BorderHelper), new PropertyMetadata(new Thickness(0), BorderHelper.TopBorderThicknessChangedCallback));
            BorderHelper.BottomBorderThicknessProperty = DependencyProperty.RegisterAttached("BottomBorderThickness", typeof(Thickness), typeof(BorderHelper), new PropertyMetadata(new Thickness(0), BorderHelper.BottomBorderThicknessChangedCallback));
        }

        /// <summary>
        /// 默认构造器
        /// </summary>
        protected BorderHelper()
        {
        }

        #endregion

        #region # 附加属性 

        #region 上侧边框宽度 —— Thickness TopBorderThickness
        /// <summary>
        /// 上侧边框宽度附加属性
        /// </summary>
        public static DependencyProperty TopBorderThicknessProperty;

        /// <summary>
        /// 获取上侧边框宽度
        /// </summary>
        /// <param name="dependencyObject"></param>
        /// <returns></returns>
        [AttachedPropertyBrowsableForType(typeof(Control))]
        [AttachedPropertyBrowsableForType(typeof(Border))]
        public static Thickness GetTopBorderThickness(DependencyObject dependencyObject)
        {
            return (Thickness)dependencyObject.GetValue(BorderHelper.TopBorderThicknessProperty);
        }

        /// <summary>
        /// 设置上侧边框宽度
        /// </summary>
        /// <param name="dependencyObject"></param>
        /// <param name="value"></param>
        [AttachedPropertyBrowsableForType(typeof(Control))]
        [AttachedPropertyBrowsableForType(typeof(Border))]
        public static void SetTopBorderThickness(DependencyObject dependencyObject, Thickness value)
        {
            dependencyObject.SetValue(BorderHelper.TopBorderThicknessProperty, value);
        }
        #endregion

        #region 下侧边框宽度 —— Thickness BottomBorderThickness
        /// <summary>
        /// 下侧边框宽度附加属性
        /// </summary>
        public static DependencyProperty BottomBorderThicknessProperty;

        /// <summary>
        /// 获取下侧边框宽度
        /// </summary>
        /// <param name="dependencyObject"></param>
        /// <returns></returns>
        [AttachedPropertyBrowsableForType(typeof(Control))]
        [AttachedPropertyBrowsableForType(typeof(Border))]
        public static Thickness GetBottomBorderThickness(DependencyObject dependencyObject)
        {
            return (Thickness)dependencyObject.GetValue(BorderHelper.BottomBorderThicknessProperty);
        }

        /// <summary>
        /// 设置下侧边框宽度
        /// </summary>
        /// <param name="dependencyObject"></param>
        /// <param name="value"></param>
        [AttachedPropertyBrowsableForType(typeof(Control))]
        [AttachedPropertyBrowsableForType(typeof(Border))]
        public static void SetBottomBorderThickness(DependencyObject dependencyObject, Thickness value)
        {
            dependencyObject.SetValue(BorderHelper.BottomBorderThicknessProperty, value);
        }
        #endregion

        #region 左侧边框宽度 —— Thickness LeftBorderThickness
        /// <summary>
        /// 左侧边框宽度附加属性
        /// </summary>
        public static DependencyProperty LeftBorderThicknessProperty;

        /// <summary>
        /// 获取左侧边框宽度
        /// </summary>
        /// <param name="dependencyObject"></param>
        /// <returns></returns>
        [AttachedPropertyBrowsableForType(typeof(Control))]
        [AttachedPropertyBrowsableForType(typeof(Border))]
        public static Thickness GetLeftBorderThickness(DependencyObject dependencyObject)
        {
            return (Thickness)dependencyObject.GetValue(BorderHelper.LeftBorderThicknessProperty);
        }

        /// <summary>
        /// 设置左侧边框宽度
        /// </summary>
        /// <param name="dependencyObject"></param>
        /// <param name="value"></param>
        [AttachedPropertyBrowsableForType(typeof(Control))]
        [AttachedPropertyBrowsableForType(typeof(Border))]
        public static void SetLeftBorderThickness(DependencyObject dependencyObject, Thickness value)
        {
            dependencyObject.SetValue(BorderHelper.LeftBorderThicknessProperty, value);
        }
        #endregion

        #region 右侧边框宽度 —— Thickness RightBorderThickness
        /// <summary>
        /// 右侧边框宽度附加属性
        /// </summary>
        public static DependencyProperty RightBorderThicknessProperty;

        /// <summary>
        /// 获取右侧边框宽度
        /// </summary>
        /// <param name="dependencyObject"></param>
        /// <returns></returns>
        [AttachedPropertyBrowsableForType(typeof(Control))]
        [AttachedPropertyBrowsableForType(typeof(Border))]
        public static Thickness GetRightBorderThickness(DependencyObject dependencyObject)
        {
            return (Thickness)dependencyObject.GetValue(BorderHelper.RightBorderThicknessProperty);
        }

        /// <summary>
        /// 设置右侧边框宽度
        /// </summary>
        /// <param name="dependencyObject"></param>
        /// <param name="value"></param>
        [AttachedPropertyBrowsableForType(typeof(Control))]
        [AttachedPropertyBrowsableForType(typeof(Border))]
        public static void SetRightBorderThickness(DependencyObject dependencyObject, Thickness value)
        {
            dependencyObject.SetValue(BorderHelper.RightBorderThicknessProperty, value);
        }
        #endregion

        #endregion

        #region # 路由事件

        #endregion

        #region # 事件处理程序

        #endregion

        #region # 回调方法

        #region 上侧边框宽度改变回调方法 —— static void TopBorderThicknessChangedCallback(DependencyObject dependencyObject...
        /// <summary>
        /// 上侧边框宽度改变回调方法
        /// </summary>
        protected static void TopBorderThicknessChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
        {
            Thickness newValue = (Thickness)eventArgs.NewValue;

            BorderHelper.SetBorderThickness(dependencyObject, newValue, Direction.Top);
        }
        #endregion

        #region 下侧边框宽度改变回调方法 —— static void BottomBorderThicknessChangedCallback(DependencyObject dependencyObject...
        /// <summary>
        /// 下侧边框宽度改变回调方法
        /// </summary>
        protected static void BottomBorderThicknessChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
        {
            Thickness newValue = (Thickness)eventArgs.NewValue;

            BorderHelper.SetBorderThickness(dependencyObject, newValue, Direction.Bottom);
        }
        #endregion

        #region 左侧边框宽度改变回调方法 —— static void LeftBorderThicknessChangedCallback(DependencyObject dependencyObject...
        /// <summary>
        /// 左侧边框宽度改变回调方法
        /// </summary>
        protected static void LeftBorderThicknessChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
        {
            Thickness newValue = (Thickness)eventArgs.NewValue;

            BorderHelper.SetBorderThickness(dependencyObject, newValue, Direction.Left);
        }
        #endregion

        #region 右侧边框宽度改变回调方法 —— static void RightBorderThicknessChangedCallback(DependencyObject dependencyObject...
        /// <summary>
        /// 右侧边框宽度改变回调方法
        /// </summary>
        protected static void RightBorderThicknessChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs eventArgs)
        {
            Thickness newValue = (Thickness)eventArgs.NewValue;

            BorderHelper.SetBorderThickness(dependencyObject, newValue, Direction.Right);
        }
        #endregion

        #endregion

        #region # 方法


        // Private 

        #region 右侧边框宽度改变回调方法 —— static void SetBorderThickness(DependencyObject dependencyObject, Thickness thickness...
        /// <summary>
        /// 右侧边框宽度改变回调方法
        /// </summary>
        protected static void SetBorderThickness(DependencyObject dependencyObject, Thickness thickness, Direction direction)
        {

            switch (dependencyObject)
            {
                case Control control:

                    control.BorderThickness = BorderHelper.GetBorderThickness(control.BorderThickness, thickness, direction);

                    break;
                case Border border:

                    border.BorderThickness = BorderHelper.GetBorderThickness(border.BorderThickness, thickness, direction);

                    break;
            }

        }
        #endregion

        #region 右侧边框宽度改变回调方法 —— static void SetBorderThickness(DependencyObject dependencyObject, Thickness thickness...
        /// <summary>
        /// 右侧边框宽度改变回调方法
        /// </summary>
        protected static Thickness GetBorderThickness(Thickness thickness, Thickness directionThickness, Direction direction)
        {
            return direction switch
            {
                Direction.Left => new Thickness(directionThickness.Left, thickness.Top, thickness.Right, thickness.Bottom),
                Direction.Top => new Thickness(thickness.Left, directionThickness.Top, thickness.Right, thickness.Bottom),
                Direction.Right => new Thickness(thickness.Left, thickness.Top, directionThickness.Right, thickness.Bottom),
                Direction.Bottom => new Thickness(thickness.Left, thickness.Top, thickness.Right, directionThickness.Bottom),
                _ => thickness
            };
        }
        #endregion

        #endregion
    }
}